<?php

/*

  Action - Attack	$5

  +$2
  Each other player discards the top card of his deck. If it’s a Victory card he gains a Curse.
  Otherwise either he gains a copy of the discarded card or you do, your choice.

 */

class Jester extends Card implements iAction, iKingdom, iAttack, iCornucopia {

    public function __construct() {
        parent::__construct("Jester", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+$2",
            "Each other player discards the top card of his deck. If it’s a Victory card he gains a Curse. Otherwise either he gains a copy of the discarded card or you do, your choice.");
    }

    public function play(Game $game, $isRealCard) {

        $game->addMoney(new Price(2));
        ScreenDisplay::addScreenMessage($game, true, "get +2$", "gets +2$");

        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $this->helper($game, $name);
        }

        $game->cardActionFinished();
        return true;
    }

    private function helper(Game $game, $name) {

        $card = $game->peekCardFromDrawDeck($name);
        if ($card) {
            if ($card instanceof iVictory) {
                //discarding the card and adding a curse
                $game->discardCardsFromDrawDeck(1, $name);
                $curse = $game->moveCardFromPileToPile("Curse", BoardCards, DiscardPile, $name);
                ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($card) . " and gain " . nicePrint($curse), "reveals " . nicePrint($card) . " and gains " . nicePrint($curse), $name);
            } else {
                ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($card), "reveals " . ScreenDisplay::$LMSG, $name);
                $options = array(new Option(1, "You"), new Option(2, $name));

                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Who will gain " . $card->getValue() . " ? ");

                //putting the first copy of the card to the discard
                $game->discardCardsFromDrawDeck(1, $name);

                //getting from the board the second copy
                if ($output[0] == 'You') {
                    $myName = $game->getCurrentPlayerName();
                    $card = $game->moveCardFromPileToPile($card, BoardCards, DiscardPile);
                    ScreenDisplay::addScreenMessage($game, false, " and you gain " . nicePrint($card), " and  $myName gains " . ScreenDisplay::$LMSG);
                } else {
                    $card = $game->moveCardFromPileToPile($card, BoardCards, DiscardPile, $name);
                    ScreenDisplay::addScreenMessage($game, false, " and you gain " . nicePrint($card), " and  $name gains " . ScreenDisplay::$LMSG, $name);
                }
            }
        }
        return true;
    }

}

?>